//
//  MJMeViewController.m
//  MJ
//
//  Created by lina on 2017/12/7.
//  Copyright © 2017年 lina. All rights reserved.
//

#import "MJMeViewController.h"
#import "MJMyInfoTableViewCell.h"
#import "MJMyInfoTableHeaderView.h"
#import "MJModifyPersonalInfoController.h"
#import "MJMoreExcitingController.h"
#import "MJMyExamController.h"
#import "MJMyInfoViewModel.h"
#import "MJMeInfoModel.h"

@interface MJMeViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) NSArray *titlesArr;
@property (nonatomic, strong) MJMyInfoTableHeaderView *headerView;

@end

@implementation MJMeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navgationBar.titleLabel.text = @"我的";
    self.view.backgroundColor = RGBColor(242,242,242);
    [self commonInit];
}
- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    [self refreshUI];
}
- (void)commonInit{
    
    [self.view addSubview:self.myTableView];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    self.myTableView.tableFooterView = [[UIView alloc] init];
    _headerView = [[MJMyInfoTableHeaderView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 185)];
    self.myTableView.tableHeaderView = _headerView;
    
    self.titlesArr = @[@[@"我的测试",@"修改资料"],@[@"更多精彩"]];
    [self.myTableView reloadData];
}
- (void)refreshUI{
    
    NSString *sex = [[NSUserDefaults standardUserDefaults] objectForKey:MJ_Sex];
    NSString *headerPath = [[NSUserDefaults standardUserDefaults] objectForKey:MJ_HeaderImage];
    if ((sex.length != 0)&&([sex isEqualToString:@"2"])) {
        
        self.headerView.sexLabel.text = @"女";
        if (headerPath.length !=0) {
            
            [self.headerView.headerImageView sd_setImageWithURL:[NSURL URLWithString:headerPath]];
        }else{
            
            self.headerView.headerImageView.image = [UIImage imageNamed:@"girl"];
        }
    }else{
        self.headerView.sexLabel.text = @"男";
        if (headerPath.length !=0) {
            
            [self.headerView.headerImageView sd_setImageWithURL:[NSURL URLWithString:headerPath]];
        }else{
            
            self.headerView.headerImageView.image = [UIImage imageNamed:@"boy"];
        }
    }
    NSString *name = [[NSUserDefaults standardUserDefaults] objectForKey:MJ_Name];
    if (name.length == 0) {
        
        self.headerView.nameLabel.text = @"神秘人";
    }else{
        
        self.headerView.nameLabel.text = name;
    }
}
//- (void)requestMyInfo{
//    
//    [MJMyInfoViewModel requestMyInfoSuccess:^(NSString *stateCode, MJMeInfoModel *infoModel, NSString *messageState) {
//        
//        if ([stateCode isEqualToString:@"001"])
//        {
//            self.headerView.infoModel = infoModel;
//        }
//        else if ([stateCode isEqualToString:@"203"])
//        {
//            MJLoginViewController *loginController = [[MJLoginViewController alloc] init];
//            [self.navigationController pushViewController:loginController animated:YES];
//        }
//        else
//        {
//            [TJMBProgressView showTingJianText:messageState];
//        }
//        
//    } failure:^(NSError *error) {
//        
//        [TJMBProgressView showTingJianText:networkError];
//        
//    } netWorkState:^(BOOL netWorkState) {
//        
//    }];
//}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return section == 0 ? 2 : 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    static NSString *cellID = @"cellID";
    MJMyInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell)
    {
        cell = [[MJMyInfoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.markLabel.text = self.titlesArr[indexPath.section][indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if ((indexPath.section == 0)&&(indexPath.row == 0)) {
        
        MJMyExamController *examController = [[MJMyExamController alloc] init];
        [self.navigationController pushViewController:examController animated:YES];
        
    }
    else if ((indexPath.section == 0)&&(indexPath.row == 1)){
        
        MJModifyPersonalInfoController *modifyController = [[MJModifyPersonalInfoController alloc] init];
        [self.navigationController pushViewController:modifyController animated:YES];
    }
    else{
        
        MJMoreExcitingController *moreController = [[MJMoreExcitingController alloc] init];
        [self.navigationController pushViewController:moreController animated:YES];

    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 55;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return section == 0 ? 0.01f : 10.f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    if (section == 1)
    {
        UIView *clearView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 10)];
        clearView.backgroundColor = [UIColor clearColor];
        return clearView;
    }
    return [UIView new];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    return [UIView new];
}
- (UITableView *)myTableView{
    
    if (!_myTableView)
    {
        _myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.navgationBar.height+15, kScreenWidth, kScreenHeight-15-64) style:UITableViewStyleGrouped];
        _myTableView.backgroundColor = RGBColor(242,242,242);
        _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _myTableView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
